/*==================================================
  probabilityWheel.js by Somik Raha
  somik@valuefound.org

  Official Home of the Probability Wheel
  http://www.valuefound.org

  License (http://www.gnu.org/licenses/)

  Copyright (c) 2011 Somik Raha
  
    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see http://www.gnu.org/licenses/.
  ==================================================*/
function ProbabilityWheel(descriptor,callback,distinctionBetString,questionObject,prefix,graphName) {
	this.low = 0;
	this.high = 1;
	this.med = (this.low+this.high)/2;
	this.layout = null;
	this.plotter = null;
	this.graphName = graphName;
	this.questionObject = questionObject;
	this.descriptor = descriptor;
	this.distinction = descriptor.assessmentPageLabel;
	this.callback = callback;
	this.resetProbabilityWheel = resetProbabilityWheel;
	this.drawPieGraph = drawPieGraph;
	this.moreOrange = moreOrange;
	this.lessOrange = lessOrange;
	this.updateWheel = updateWheel;
	this.indifferent = indifferent;
	this.getTimeStamp = getTimeStamp;
	this.setNewBet = setNewBet;
	this.distinctionBetID = "distinctionBet"+Math.round(Math.random()*100);
	if (typeof(prefix)=='undefined') {
		this.prefix = "";
	} else {
		this.prefix = prefix;
	}
	this.createHtml = createHtml;
	this.questionObject.innerHTML = this.createHtml();
	this.setNewBet(distinctionBetString);
};

function setNewBet(newBetString) {
	document.getElementById(this.distinctionBetID).innerHTML = newBetString;
}

function createHtml() {
	return "<h1>"+this.distinction+"</h1>" +
	"Let's play a game. You can either choose to bet on <b><span id='"+this.distinctionBetID+"'>&nbsp;</span></b> or on <b>the wheel below landing orange.</b> " +
	"Depending on what you bet on, if you win, you get $2 billion. There is no charge for playing this game, but you can play only once, and since the stake is so high, you will want to think carefully about this.<p/>" +
	"You now have to make a decision: " +
		"<div id=\""+this.graphName+"\" style=\"width: 300px; height: 300px;\"></div>"+
		"<div id=\""+this.graphName+"legend\"></div>"+
	"<form>" +
		"<input type=button value='Bet on wheel landing orange' onClick='"+this.prefix+"pWheel.moreOrange();'/>" +
		"<input type=button value='Bet on "+this.distinction+"' onClick='"+this.prefix+"pWheel.lessOrange();'/>" +
		"<input type=button value='Reset' onClick='"+this.prefix+"pWheel.resetProbabilityWheel();'/>" +
		"<input type=button value='Indifferent' onClick='"+this.prefix+"pWheel.indifferent();'/>" +
	"</form>" +
	"<span id='resultOfAssessment'></span>";
}

function resetProbabilityWheel() {
	this.high = 1;
	this.low = 0;
	this.med = 0.5;
	this.updateWheel();
}

function drawPieGraph() {
	dojo.require("dojox.charting.Chart2D");
	dojo.require("dojox.charting.plot2d.Pie");
	dojo.require("dojox.charting.action2d.Highlight");
	dojo.require("dojox.charting.action2d.MoveSlice");
	dojo.require("dojox.charting.action2d.Tooltip");
	dojo.require("dojox.charting.themes.MiamiNice");
	dojo.require("dojox.charting.widget.Legend");
	var gName = this.graphName;
	dojo.addOnLoad(function(){
	  var dc = dojox.charting;
	  dojo.pieChart = new dc.Chart2D(gName);
	  dojo.pieChart.setTheme(dc.themes.MiamiNice)
	     .addPlot("default", {
	        type: "Pie",
	        font: "normal normal 11pt Tahoma",
	        fontColor: "black",
	        labelOffset: -30,
	        radius: 80,
	        startAngle: -90
	  }).addSeries("Series A", [
	      {y: 4, text: "$2 billion",   stroke: "black", color: "red", tooltip: "You win $2 billion if the wheel lands here"},
	      {y: 4, text: "Nothing", stroke: "black", color: "blue", tooltip: "You win nothing if the wheel lands here"}    
	  ]);
	  var anim_a = new dc.action2d.MoveSlice(dojo.pieChart, "default");
	  var anim_b = new dc.action2d.Highlight(dojo.pieChart, "default");
	  var anim_c = new dc.action2d.Tooltip(dojo.pieChart, "default");
	  dojo.pieChart.render();
	  var legend = new dojox.charting.widget.Legend({chart: dojo.pieChart}, this.graphName+"legend");
	});
}
 
function moreOrange() {
	this.low = this.med;
	this.updateWheel();
}

function lessOrange() {
	this.high = this.med;
	this.updateWheel();
} 

function updateWheel() {
   this.med = (this.low + this.high) /2;
   dojo.pieChart.updateSeries("Series A",[{y: 1-this.med, text: "$2 billion",   stroke: "black", color: "red", tooltip: "Red is 50%"},
	                                       {y: this.med, text: "Nothing", stroke: "black", color: "blue", tooltip: "Green is 50%"}]);
   dojo.pieChart.render();
}

function indifferent() {
	var probability = (1-this.med)*100;
	this.callback(this.descriptor, probability);
}

function getTimeStamp() {
	Stamp = new Date();
	answer = (Stamp.getMonth() + 1) +"/"+Stamp.getDate()+ "/"+Stamp.getFullYear();
	var Hours;
	var Mins;
	var Time;
	Hours = Stamp.getHours();
	if (Hours >= 12) {
		Time = " P.M.";
	}
	else {
		Time = " A.M.";
	}
	if (Hours > 12) {
		Hours -= 12;
	}
	if (Hours == 0) {
		Hours = 12;
	}
	Mins = Stamp.getMinutes();
	if (Mins < 10) {
		Mins = "0" + Mins;
	}	
	answer += " "+Hours + ":" + Mins + Time ;
    return answer;
}
